﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BonsaiScrum.Core.Domain.Shared;
using System.Collections.ObjectModel;

namespace BonsaiScrum.Core.Domain
{
    public class Story : BaseEntity<Story>
    {
        public virtual string Description { get; private set; }

        private Project _project;
        private IList<Task> _tasks;

        protected Story() 
        {
            _tasks = new List<Task>();
        }

        public Story(string description, Project project) : this()
        {
            CheckForValidDescription(description);

            Description = description;
            _project = project;
        }

        public virtual void ChangeDescription(string newDescription)
        {
            CheckForValidDescription(newDescription);

            Description = newDescription;
        }

        private void CheckForValidDescription(string description)
        {
            if (string.IsNullOrEmpty(description))
                throw new InvalidOperationException("A story cannot have an empty or null description.");
        }

        public virtual IList<Task> Tasks
        {
            get { return new ReadOnlyCollection<Task>(_tasks); }
        }

        public virtual bool AllTasksAreCompleted()
        {
            return _tasks.All(x => x.IsCompleted());
        }

        public virtual void AddTask(string taskDescription, Priority priority, int estimatedHoursToComplete)
        {
            var newTask = new Task(taskDescription, priority, estimatedHoursToComplete);

            _tasks.Add(newTask);
        }
    }
}
